package com.xywy.fwc.widget;


import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.xywy.R;

public class FwcPopWindow extends PopupWindow{
	private PopupWindow mPopupWindow;
	private Context context;
	private LinearLayout lin_pop;
	private int width;
	OnClickListener clickListener;
	OnDismissListener dismissListener;
	String [] str_week = {
			"星期一",
			"星期二",
			"星期三",
			"星期四",
			"星期五",
			"星期六",
			"星期日",
	};

	public FwcPopWindow(Context context,int width,OnClickListener clickListener,OnDismissListener dismissListener) {
		super();
		this.context = context;
		this.width = width;
		this.clickListener = clickListener;
		this.dismissListener = dismissListener;
		init();
	}
	private void init(){
		View view = View.inflate(context, R.layout.fwc_pop_window, null);
		lin_pop = (LinearLayout) view.findViewById(R.id.lin_pop);
		mPopupWindow = new PopupWindow(view, width,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		for(int i=0;i<str_week.length;i++){
			View view_child = View.inflate(context, R.layout.fwc_pop_item, null);
			View v = view_child.findViewById(R.id.bottom);
			view_child.setTag(i);
			if(i !=6){
				v.setVisibility(View.VISIBLE);
			}else {
				v.setVisibility(View.INVISIBLE);
			}
			TextView tv = (TextView) view_child.findViewById(R.id.tv_week);
			tv.setText(str_week[i]);
			lin_pop.addView(view_child);
			view_child.setOnClickListener(clickListener);
		}
		mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
		mPopupWindow.setOnDismissListener(dismissListener);
	}
	
	public void show(View parent, int gravity, int x, int y){
		mPopupWindow.showAsDropDown(parent, x, y);
	}
	
	
	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
